<?php
/**
 * 数据统计
 */
namespace App;
defined('IN_IA') or exit('Access Denied');

use Model\InstallMod;
use Model\MemberMod;
use Model\RedMod;

class Info extends Action
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 获取总数据和今日数据
     */
    public function getTotalTodayData()
    {
        $_data = array(
            "total"=>$this->getDateData(),
            "today"=>$this->getDateData(strtotime(date('Y-m-d')))
        );
        $this->_result = array("status"=>"success","data"=>$_data);
        $this->result($this->_result);
    }

    /**
     * 获取数据总数
     */
    public function getTotal()
    {
        $Install = new InstallMod();
        $_limit_time = $Install->findOne();
        $_limit_time = strtotime(date("Y-m-d",$_limit_time["time"]));
        $_start_time = strtotime(date("Y-m-d")) - 86400;
        $_total = ($_start_time - $_limit_time) / 86400+1;
        $this->_result = array("status"=>"success","data"=>$_total);
        $this->result($this->_result);
    }

    /**
     * 获取列表
     * @param array limit 请求下标
     */
    public function getList()
    {
        $_limit = $this->_gpc["limit"];
        $Install = new InstallMod();
        $_limit_time = $Install->findOne();
        $_limit_time = strtotime(date("Y-m-d",$_limit_time["time"]));
        $_start_time = strtotime(date("Y-m-d"));
        $_list_start_time = $_start_time - 86400*$_limit[0];
        $_list_end_time = $_list_start_time - 86400 * $_limit[1];
        $_list_end_time = $_list_end_time <= $_limit_time ? $_limit_time : $_list_end_time;
        $_data = array();
        for($i=$_list_start_time;$i>$_list_end_time;$i-=86400){
            $_temp_data = array("time"=>date("Y-m-d",$i));
            $_data[] = array_merge($_temp_data,$this->getDateData($i));
        }
        if(count($_data) > 0){
            $this->_result = array("status"=>"success","data"=>$_data);
        }
        $this->result($this->_result);
    }

    /**
     * 获取指定日期统计数据
     * @param int $_time 指定数据 1=全部数据
     * @return array 返回统计数据
     */
    private function getDateData($_time = 1)
    {
        $_data = array();
        $_end_time = $_time === 1 ? 4102416000 : $_time+86400-1;
        //会员数量
        $Member = new MemberMod();
        $_data['member_number'] = $Member->total(
            array(
                "uniacid"=>$this->_uniacid,
                "register_time >"=>$_time,
                "register_time <="=>$_end_time
            )
        );
        //发红包数量
        $Red = new RedMod();
        $_data["publish_red"] = $Red->total(
            array(
                "uniacid"=>$this->_uniacid,
                "time >"=>$_time,
                "time <="=>$_end_time,
                "status >"=>1
            )
        );
        //发红包金额
            //普通红包总额
        $_data["red_sum"] = $Red->redInfo($_time,$_end_time,$this->_uniacid);
            //拼手气红包总额
        $_data["red_sum"] += $Red->sum(array("uniacid"=>$this->_uniacid,"type"=>1,"time >"=>$_time,"time <="=>$_end_time,"status >"=>1),"money");
        //收红包手续费（收入）
        $_red_income = $Red->sum(
            array(
                "uniacid"=>$this->_uniacid,
                "time >"=>$_time,
                "time <="=>$_end_time,
                "status >"=>1
            ),
            'charge'
        );
        $_data['red_income'] = $_red_income == 0 ? "0.00" : $_red_income;
        return $_data;
    }

}